package com.zb.excel.importExcel.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.zb.constant.RedisImportDataConstant;
import com.zb.entity.*;
import com.zb.excel.*;
import com.zb.excel.importExcel.listener.*;
import com.zb.excel.importExcel.service.ConImportExcelService;
import com.zb.redis.RedisUtil;
import com.zb.service.*;
import com.zb.view.ViewGoods;
import com.zb.websocket.server.ProgressWebSocketServer;
import lombok.extern.log4j.Log4j2;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Author WEN
 * @Date 2024/1/5 0005
 **/
@Log4j2
@Service
public class ConImportExcelServiceImpl implements ConImportExcelService {
    @Resource(name = "asyncServiceExecutor")
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Resource
    private PlatformTransactionManager transactionManager;
    @Resource
    private ZjUserSetService userSetService;
    @Resource
    private ZjUserService userService;
    @Resource
    private ZjUserContactService userContactService;
    @Resource
    private ZjUserTypeService userTypeService;
    @Resource
    private ZjUserLevelService userLevelService;
    @Resource
    private ZjBrandService brandService;
    @Resource
    private ZjGoodsUnitService goodsUnitService;
    @Resource
    private ZjGoodsTypeService goodsTypeService;
    @Resource
    private ZjProductService productService;
    @Resource
    private ZjProductSpecService productSpecService;
    @Resource
    private ZjSpareService spareService;
    @Resource
    private ZjSpareSpecInfoService specInfoService;
    @Resource
    private ZjAccountService accountService;
    @Resource
    private ZjEmpService empService;
    @Resource
    private ZjNetworkService networkService;
    @Resource
    private ZjNetworkEmpService networkEmpService;
    @Resource
    private ZjNetworkEmpRelationService networkEmpRelationService;
    @Resource
    private ZjNetworkEmpLevelService networkEmpLevelService;
    @Resource
    private ZjNetworkEmpSkillService networkEmpSkillService;
    @Resource
    private ZjNetworkEmpTypeService networkEmpTypeService;
    @Resource
    private ZjNetworkEmpLevelRelationService networkEmpLevelRelationService;
    @Resource
    private ZjNetworkEmpSkillRelationService networkEmpSkillRelationService;
    @Resource
    private ZjNetworkTypeService networkTypeService;
    @Resource
    private ZjNetworkStrategyService networkStrategyService;
    @Resource
    private ZjNetworkPathService networkPathService;
    @Resource
    private ZjWorkLevelService workLevelService;
    @Resource
    private ZjWorkModuleService workModuleService;
    @Resource
    private ZjCodeBatchService codeBatchService;
    @Resource
    private ZjCodeService codeService;
    @Resource
    private ZjCodeUserRegisterService codeUserRegisterService;
    @Resource
    private ViewGoodsService viewGoodsService;
    @Resource
    private ZjCodeSetService codeSetService;
    @Resource
    private ZjWorkOrderService workOrderService;
    @Resource
    private ZjWorkDetailService workDetailService;
    @Resource
    private ZjWorkUserService workUserService;
    @Resource
    private ZjWorkGoodsService workGoodsService;
    @Resource
    private ZjWorkGoodsCodeService workGoodsCodeService;
    @Resource
    private ZjFactoryService factoryService;
    @Resource
    private ZjFactoryOrderModuleSettlementService factoryOrderModuleSettlementService;
    @Resource
    private ViewNetworkEmpDetailService viewNetworkEmpDetailService;
    @Resource
    private ZjWorkDispatchNetworkService workDispatchNetworkService;
    @Resource
    private ZjWorkDispatchEngineerService workDispatchEngineerService;


    // 客户档案导入
    @Override
    public void userExcelImport(AccounLoginInfo loginInfo, String uuid, MultipartFile file) {
        threadPoolTaskExecutor.execute(() -> {
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                Map<String, Object> cacheMap = new HashMap<>();
                QueryWrapper<ZjUser> userQueryWrapper = new QueryWrapper<>();
                userQueryWrapper.select("id", "code", "phone");
                userQueryWrapper.eq("cust_acc_id", loginInfo.getCustAccId());
                List<ZjUser> userList = userService.list(userQueryWrapper);
                userList.forEach(user -> {
                    cacheMap.put("code_" + user.getCode(), user.getId());
                    cacheMap.put("account_" + user.getPhone(), user);
                });
                List<Long> userIds = userList.stream().map(ZjUser::getId).toList();
                if (CollectionUtil.isNotEmpty(userIds)) {
                    QueryWrapper<ZjUserContact> contactQueryWrapper = new QueryWrapper<>();
                    contactQueryWrapper.select("id", "user_id", "username", "phone")
                            .in("user_id", userIds)
                            .eq("is_main_contact", true);
                    userContactService.list(contactQueryWrapper).forEach(t -> {
                        cacheMap.put("contact_" + t.getUserId(), t);
                    });
                }
                QueryWrapper<ZjUserType> typeQueryWrapper = new QueryWrapper<>();
                typeQueryWrapper.select("id", "name");
                typeQueryWrapper.in("cust_acc_id", loginInfo.getCustAccId());
                userTypeService.list(typeQueryWrapper).forEach(type -> {
                    cacheMap.put("type_" + type.getName(), type);
                });
                QueryWrapper<ZjUserLevel> levelQueryWrapper = new QueryWrapper<>();
                levelQueryWrapper.select("id", "name");
                levelQueryWrapper.eq("cust_acc_id", loginInfo.getCustAccId());
                userLevelService.list(levelQueryWrapper).forEach(level -> {
                    cacheMap.put("level_" + level.getName(), level);
                });
                String userPrefix = userSetService.getCodeById(loginInfo.getCustAccId());
                if (!cacheMap.isEmpty()) {
                    RedisUtil.setCache(RedisImportDataConstant.USER_IMPORT + loginInfo.getCustAccId() + uuid, JSON.toJSONString(cacheMap));
                }
                EasyExcel.read(inputStream, UserExcelImport.class, new UserExcelImportListener(loginInfo, userPrefix, uuid, transactionManager,
                        userService, userContactService, userTypeService, userLevelService)).headRowNumber(2).doReadAll();
            } catch (Exception e) {
                log.error("导入客户档案失败：" + e);
                ProgressWebSocketServer.send(uuid, true, new ArrayList(), "数据解析失败，请检查数据后重新导入");
                e.printStackTrace();
            } finally {
                RedisUtil.clearCache(RedisImportDataConstant.USER_IMPORT + loginInfo.getCustAccId() + uuid);
                if (null != inputStream) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {}
                }
            }
        });
    }

    // 商品资料导入
    @Override
    public void goodsExcelImport(AccounLoginInfo loginInfo, int type, String uuid, MultipartFile file) {
        threadPoolTaskExecutor.execute(() -> {
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                Map<String, Object> cacheMap = new HashMap<>();
                QueryWrapper<ZjBrand> brandQueryWrapper = new QueryWrapper<>();
                brandQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                brandService.list(brandQueryWrapper).forEach(t -> {
                    cacheMap.put("goods_brand_" + t.getName(), t);
                });

                QueryWrapper<ZjGoodsType> typeQueryWrapper = new QueryWrapper<>();
                typeQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                goodsTypeService.list(typeQueryWrapper).forEach(t -> {
                    cacheMap.put("goods_type_" + t.getName(), t);
                });

                QueryWrapper<ZjGoodsUnit> unitQueryWrapper = new QueryWrapper<>();
                unitQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                goodsUnitService.list(unitQueryWrapper).forEach(t -> {
                    cacheMap.put("goods_unit_" + t.getName(), t);
                });
                if (type == 1) {
                    getCacheProductData(cacheMap, loginInfo.getCustAccId());
                }
                if (type == 2) {
                    QueryWrapper<ZjSpare> spareQueryWrapper = new QueryWrapper<>();
                    spareQueryWrapper.select("id", "model", "name", "code", "item_number")
                            .eq("cust_acc_id", loginInfo.getCustAccId());
                    List<ZjSpare> spareList = spareService.list(spareQueryWrapper);
                    spareList.forEach(t -> {
                        cacheMap.put("goods_spare_model_" + t.getModel(), t);
                    });
                    List<Long> spareIds = spareList.stream().map(ZjSpare::getId).toList();
                    if (CollectionUtil.isNotEmpty(spareIds)) {
                        QueryWrapper<ZjSpareSpecInfo> spareSpecInfoQueryWrapper = new QueryWrapper<>();
                        spareSpecInfoQueryWrapper.select("id", "spare_id", "name", "code")
                                .in("spare_id", spareIds);
                        specInfoService.list(spareSpecInfoQueryWrapper).forEach(t -> {
                            cacheMap.put("goods_spare_spec_name_" + t.getSpareId() + t.getName(), t);
                        });
                    }
                }
                if (!cacheMap.isEmpty()) {
                    RedisUtil.setCache(RedisImportDataConstant.GOODS_IMPORT + loginInfo.getCustAccId() + uuid, JSON.toJSONString(cacheMap));
                }
                EasyExcel.read(inputStream, GoodsExcelImport.class, new GoodsExcelImportListener(loginInfo, type, uuid, transactionManager, brandService, goodsTypeService,
                        goodsUnitService, productService, productSpecService, spareService, specInfoService)).headRowNumber(2).doReadAll();
            } catch (Exception e) {
                log.error("导入商品资料失败：" + e);
                ProgressWebSocketServer.send(uuid, true, new ArrayList(), "数据解析失败，请检查数据后重新导入");
                e.printStackTrace();
            } finally {
                RedisUtil.clearCache(RedisImportDataConstant.GOODS_IMPORT + loginInfo.getCustAccId() + uuid);
                if (null != inputStream) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {}
                }
            }
        });
    }

    /**
     * 查询并缓存商品数据
     * @param cacheMap
     * @param custAccId
     */
    private void getCacheProductData(Map<String, Object> cacheMap, Long custAccId) {
        QueryWrapper<ZjProduct> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.select("id", "model", "name", "code", "item_number")
                .eq("cust_acc_id", custAccId);
        List<ZjProduct> productList = productService.list(productQueryWrapper);
        productList.forEach(t -> {
            cacheMap.put("goods_product_model_" + t.getModel(), t);
        });
        List<Long> productIds = productList.stream().map(ZjProduct::getId).toList();
        if (CollectionUtil.isNotEmpty(productIds)) {
            QueryWrapper<ZjProductSpec> productSpecQueryWrapper = new QueryWrapper<>();
            productSpecQueryWrapper.select("id", "product_id", "name", "code")
                    .in("product_id", productIds);
            productSpecService.list(productSpecQueryWrapper).forEach(t -> {
                cacheMap.put("goods_product_spec_name_"+ t.getProductId() + t.getName(), t);
            });
        }
    }

    // 工程师资料导入
    @Override
    public void networkEmpImport(AccounLoginInfo loginInfo, String uuid, MultipartFile file) {
        threadPoolTaskExecutor.execute(() -> {
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                Map<String, Object> cacheMap = new HashMap<>();
                networkDataCache(loginInfo.getCustAccId(), cacheMap);
                QueryWrapper<ZjNetworkEmpLevel> empLevelQueryWrapper = new QueryWrapper<>();
                empLevelQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                networkEmpLevelService.list(empLevelQueryWrapper).forEach(t -> {
                    cacheMap.put("network_emp_level_" + t.getName(), t);
                });

                QueryWrapper<ZjNetworkEmpSkill> empSkillQueryWrapper = new QueryWrapper<>();
                empSkillQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                networkEmpSkillService.list(empSkillQueryWrapper).forEach(t -> {
                    cacheMap.put("network_emp_skill_" + t.getName(), t);
                });

                QueryWrapper<ZjNetworkEmpType> empTypeQueryWrapper = new QueryWrapper<>();
                empTypeQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                networkEmpTypeService.list(empTypeQueryWrapper).forEach(t -> {
                    cacheMap.put("network_emp_type_" + t.getName(), t);
                });

                QueryWrapper<ZjNetworkEmpLevelRelation> empLevelRelationQueryWrapper = new QueryWrapper<>();
                empLevelRelationQueryWrapper.select("id", "cust_acc_id", "network_emp_id")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                networkEmpLevelRelationService.list(empLevelRelationQueryWrapper).forEach(t -> {
                    cacheMap.put("network_emp_level_relation_" + t.getCustAccId() + t.getNetworkEmpId(), true);
                });

                QueryWrapper<ZjNetworkEmpSkillRelation> empSkillRelationQueryWrapper = new QueryWrapper<>();
                empSkillRelationQueryWrapper.select("id", "cust_acc_id", "network_emp_id", "network_emp_skill_id")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                networkEmpSkillRelationService.list(empSkillRelationQueryWrapper).forEach(t -> {
                    cacheMap.put("network_emp_skill_relation_" + t.getCustAccId() + t.getNetworkEmpId() + t.getNetworkEmpSkillId(), t.getId());
                });

                if (!cacheMap.isEmpty()) {
                    RedisUtil.setCache(RedisImportDataConstant.NETWORK_EMP_IMPORT + loginInfo.getCustAccId() + uuid, JSON.toJSONString(cacheMap));
                }
                EasyExcel.read(inputStream, NetworkEmpImport.class, new NetworkEmpImportListener(uuid, loginInfo, transactionManager, networkEmpLevelService, networkEmpSkillService,
                        networkEmpTypeService, accountService, networkEmpService, networkEmpLevelRelationService, networkEmpSkillRelationService, networkEmpRelationService)).headRowNumber(2).doReadAll();
            } catch (Exception e) {
                log.error("导入工程师资料失败：" + e);
                ProgressWebSocketServer.send(uuid, true, new ArrayList(), "数据解析失败，请检查数据后重新导入");
                e.printStackTrace();
            } finally {
                RedisUtil.clearCache(RedisImportDataConstant.NETWORK_EMP_IMPORT + loginInfo.getCustAccId() + uuid);
                if (null != inputStream) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {}
                }
            }
        });
    }

    // 服务商资料导入
    @Override
    public void networkImport(AccounLoginInfo loginInfo, String uuid, MultipartFile file) {
        threadPoolTaskExecutor.execute(() -> {
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                Map<String, Object> cacheMap = new HashMap<>();
                networkDataCache(loginInfo.getCustAccId(), cacheMap);
                // 服务商分类
                QueryWrapper<ZjNetworkType> networkTypeQueryWrapper = new QueryWrapper<>();
                networkTypeQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                networkTypeService.list(networkTypeQueryWrapper).forEach(t -> {
                    cacheMap.put("network_type_" + t.getName(), t);
                });
                if (!cacheMap.isEmpty()) {
                    RedisUtil.setCache(RedisImportDataConstant.NETWORK_IMPORT + loginInfo.getCustAccId() + uuid, JSON.toJSONString(cacheMap));
                }
                EasyExcel.read(inputStream, NetworkImport.class, new NetworkImportListener(uuid, loginInfo, transactionManager, networkService, networkStrategyService,
                        networkPathService, networkTypeService, accountService, networkEmpService, networkEmpRelationService)).headRowNumber(3).doReadAll();
            } catch (Exception e) {
                log.error("导入服务商资料失败：" + e);
                ProgressWebSocketServer.send(uuid, true, new ArrayList(), "数据解析失败，请检查数据后重新导入");
                e.printStackTrace();
            } finally {
                RedisUtil.clearCache(RedisImportDataConstant.NETWORK_IMPORT + loginInfo.getCustAccId() + uuid);
                if (null != inputStream) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {}
                }
            }
        });
    }

    private void networkDataCache(Long custAccId, Map<String, Object> cacheMap) {
        // 服务商
        QueryWrapper<ZjNetwork> networkQueryWrapper = new QueryWrapper<>();
        networkQueryWrapper.select("id", "name")
                .eq("cust_acc_id", custAccId);
        networkService.list(networkQueryWrapper).forEach(t -> {
            cacheMap.put("network_" + t.getName(), t);
        });
        // 人员账号
        QueryWrapper<ZjAccount> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.select("id", "phone");
        accountService.list(accountQueryWrapper).forEach(t -> {
            cacheMap.put("network_emp_account_" + t.getPhone(), t);
        });
        // 服务商人员
        QueryWrapper<ZjNetworkEmp> networkEmpQueryWrapper = new QueryWrapper<>();
        networkEmpQueryWrapper.select("id", "account_id");
        networkEmpService.list(networkEmpQueryWrapper).forEach(t -> {
            cacheMap.put("network_emp_" + t.getAccountId(), t);
        });
        // 服务商人员关联
        QueryWrapper<ZjNetworkEmpRelation> empRelationQueryWrapper = new QueryWrapper<>();
        empRelationQueryWrapper.select("id", "network_id", "network_emp_id", "is_manager AS manager")
                .eq("cust_acc_id", custAccId);
        networkEmpRelationService.list(empRelationQueryWrapper).forEach(t -> {
            cacheMap.put("network_emp_relation_" + t.getNetworkId() + t.getNetworkEmpId(), t);
        });
    }

    // 工单数据导入
    @Override
    public void orderImport(AccounLoginInfo loginInfo, String uuid, MultipartFile file) {
        threadPoolTaskExecutor.execute(() -> {
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                Map<String, Object> cacheMap = new HashMap<>();
                // 品牌商家
                QueryWrapper<ZjFactory> factoryQueryWrapper = new QueryWrapper<>();
                factoryQueryWrapper.select("id", "name", "order_identifier_no")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                factoryService.list(factoryQueryWrapper).forEach(factory -> {
                    cacheMap.put("factory_" + factory.getName(), factory);
                });
                // 客户设置
                ZjUserSet userSet = userSetService.getById(loginInfo.getCustAccId());
                // 工单客户
                QueryWrapper<ZjUser> userQueryWrapper = new QueryWrapper<>();
                userQueryWrapper.select("id", "code", "phone");
                userQueryWrapper.eq("cust_acc_id", loginInfo.getCustAccId());
                userService.list(userQueryWrapper).forEach(user -> {
                    cacheMap.put("user_" + user.getPhone(), user);
                });
                // 报单商品
                viewGoodsService.getProductExportByCustAccId(loginInfo.getCustAccId()).forEach(goods -> {
                    cacheMap.put("goods_product_model_" + goods.getModel(), goods);
                });
                // 工单类型
                QueryWrapper<ZjWorkModule> moduleQueryWrapper = new QueryWrapper<>();
                moduleQueryWrapper.select("id", "name", "warranty_day", "in_sourcing_price", "out_sourcing_price")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                List<ZjWorkModule> moduleList = workModuleService.list(moduleQueryWrapper);
                moduleList.forEach(module -> {
                    cacheMap.put("order_module_" +module.getName(), module);
                });
                // 工单等级
                QueryWrapper<ZjWorkLevel> levelQueryWrapper = new QueryWrapper<>();
                levelQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                workLevelService.list(levelQueryWrapper).forEach(level -> {
                    cacheMap.put("order_level_" + level.getName(), level);
                });
                // 服务商
                QueryWrapper<ZjNetwork> networkQueryWrapper = new QueryWrapper<>();
                networkQueryWrapper.select("id", "name")
                        .eq("cust_acc_id", loginInfo.getCustAccId());
                List<ZjNetwork> networkList = networkService.list(networkQueryWrapper);
                networkList.forEach(network -> {
                    cacheMap.put("order_network_" + network.getName(), network);
                });
                // 工程师
                if (CollectionUtil.isNotEmpty(networkList)) {
                    viewNetworkEmpDetailService.getByNetworkIds(networkList.stream().map(ZjNetwork::getId).toList()).forEach(networkEmp -> {
                        cacheMap.put("order_network_engineer_" + networkEmp.getNetworkId() + "_" + networkEmp.getAccountName(), networkEmp);
                        if (networkEmp.getManager()) {
                            cacheMap.put("order_network_manager_" + networkEmp.getNetworkId(), networkEmp);
                        }
                    });
                }
                // 客服
                empService.getByCustAccId(loginInfo.getCustAccId()).forEach(emp -> {
                    cacheMap.put("order_service_emp_" + emp.getName(), emp.getId());
                });
                // 品牌商家结算价
                if (CollectionUtil.isNotEmpty(moduleList)) {
                    QueryWrapper<ZjFactoryOrderModuleSettlement> moduleSettlementQueryWrapper = new QueryWrapper<>();
                    moduleSettlementQueryWrapper.select("id", "factory_id", "module_id", "price", "is_all_inclusive_fee AS allInclusiveFee")
                            .in("module_id", moduleList.stream().map(ZjWorkModule::getId).toList());
                    factoryOrderModuleSettlementService.list(moduleSettlementQueryWrapper).forEach(t -> {
                        cacheMap.put("order_factory_module_settlement_" + t.getFactoryId() + "_" + t.getModuleId(), t);
                    });
                }
                if (!cacheMap.isEmpty()) {
                    RedisUtil.setCache(RedisImportDataConstant.ORDER_IMPORT + loginInfo.getCustAccId() + uuid, JSON.toJSONString(cacheMap));
                }
                // 编码设置
                ZjCodeSet codeSet = codeSetService.getById(loginInfo.getCustAccId());
                EasyExcel.read(inputStream, OrderExcelImport.class, new OrderExcelImportListener(uuid, loginInfo, userSet, codeSet,
                        transactionManager, codeBatchService, codeService, codeUserRegisterService, userService, userContactService, workLevelService,
                        workOrderService, workDetailService, workUserService, workGoodsService, workGoodsCodeService, workDispatchNetworkService, workDispatchEngineerService)
                ).headRowNumber(3).doReadAll();
            } catch (Exception e) {
                log.error("导入工单数据失败：" + e);
                ProgressWebSocketServer.send(uuid, true, new ArrayList(), "数据解析失败，请检查数据后重新导入");
                e.printStackTrace();
            } finally {
                RedisUtil.clearCache(RedisImportDataConstant.ORDER_IMPORT + loginInfo.getCustAccId() + uuid);
                if (null != inputStream) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
            }
        });
    }

    // 一物一码数据导入
    @Override
    public void codeImport(AccounLoginInfo loginInfo, String uuid, MultipartFile file) {
        threadPoolTaskExecutor.execute(() -> {
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                Map<String, Object> cacheMap = new HashMap<>();
                // 商品
                viewGoodsService.getGoodsExportByCustAccId(loginInfo.getCustAccId()).forEach(goods -> {
                    cacheMap.put("goods_model_" + goods.getGoodsType() + "_" + goods.getModel(), true);
                    cacheMap.put("goods_model_spec_" + goods.getGoodsType() + "_" + goods.getModel() +"_"+ goods.getSpecName(), goods);
                });
                if (!cacheMap.isEmpty()) {
                    RedisUtil.setCache(RedisImportDataConstant.SECURITY_CODE_IMPORT + loginInfo.getCustAccId() + uuid, JSON.toJSONString(cacheMap));
                }
                // 编码设置
                ZjCodeSet codeSet = codeSetService.getById(loginInfo.getCustAccId());
                EasyExcel.read(inputStream, CodeExcelImport.class, new CodeExcelImportListener(uuid, loginInfo, codeSet,
                        transactionManager, codeBatchService, codeService)
                ).headRowNumber(2).doReadAll();
            } catch (Exception e) {
                log.error("导入一物一码数据失败：" + e);
                ProgressWebSocketServer.send(uuid, true, new ArrayList(), "数据解析失败，请检查数据后重新导入");
                e.printStackTrace();
            } finally {
                RedisUtil.clearCache(RedisImportDataConstant.SECURITY_CODE_IMPORT + loginInfo.getCustAccId() + uuid);
                if (null != inputStream) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                    }
                }
            }
        });
    }


}
